<template>
  <div class="will-task">

    <el-select v-model="selectTaskTypeValue" filterable placeholder="请选择问题类型(可搜索)"
               class="select-task-type" @change="selectTaskType">
      <el-option
        v-for="item in selectTaskTypeList"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
    <el-button type="primary" icon="el-icon-delete" @click="cleanSelectTaskType"></el-button>

    <el-table :data="taskList" border stripe class="item-table">
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column align="center" prop="userType" label="用户组" width="80px"></el-table-column>
      <el-table-column align="center" prop="userId" label="用户ID" width="180px">
        <template scope="scope">
          <p style="color: #409EFF;">{{scope.row.userId}}</p>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="userInfo.realName" label="姓名" width="80px"></el-table-column>
      <el-table-column align="center" prop="userInfo.cell" label="电话" width="120px"></el-table-column>
      <el-table-column align="center" prop="taskName" label="问题类型"></el-table-column>
      <el-table-column align="center" prop="taskMessage" label="问题信息">
        <template scope="scope">
          <p style="color: firebrick;">{{scope.row.taskMessage}}</p>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="dayId" label="问题时间"></el-table-column>
      <el-table-column align="center" prop="status" label="状态" width="80px">
        <template scope="scope">
          <el-tag size="mini" type="warning">未分配</el-tag>
        </template>
      </el-table-column>
    </el-table>

    <div class="page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 15, 20, 50]"
        :current-page.sync="page.current"
        :page-size="page.size"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>

  </div>
</template>

<script>
  export default {
    components: {},
    name: 'WillTask',
    data () {
      return {
        // 问题类型搜索
        selectTaskTypeList: [],
        selectTaskTypeValue: '',

        taskList: [],
        page: {
          current: 1,
          size: 10,
          total: 0
        },
        pageIndex: 0, // 0 任务列表， 1 选择问题类型任务列表
      }
    },
    methods: {
      /** 页数修改处理 */
      handleSizeChange(val) {
        this.page.current = 1;
        this.page.size = val;
        this.loadTaskList();
      },
      handleCurrentChange() {
        this.loadTaskList();
      },

      loadTaskList() {
        let url = '';
        let param = {};
        switch (this.pageIndex) {
          case 1: {
            url = "/api/task/page/list/taskcode/1";
            param = { taskCode: this.selectTaskTypeValue, current: this.page.current, size: this.page.size };
            break;
          }
          case 0:
          default: {
            url = "/api/task/page/list/1";
            param = { current: this.page.current, size: this.page.size };
            break;
          }
        }

        this.$http.post(url, param, {emulateJSON: true}).then((res) => {
          if (!this.checkResponse(res)) {
            return;
          }

          let page = res.body.data;
          this.page.total = page.total;
          this.taskList = page.data;
        }).catch((e) => { this.messageException(e); });

      },

      loadSelectTaskTypeList() {
        this.$http.get("/api/type/list", {}, {emulateJSON: true}).then((res) => {
          if (!this.checkResponse(res)) {
            return;
          }

          this.selectTaskTypeList = [];
          let typeList = res.body.data;
          for (let i = 0; i < typeList.length; i++) {
            this.selectTaskTypeList.push({
              value: typeList[i].taskCode,
              label: '(' + (i+1) + ') ' + typeList[i].taskName
            });
          }
        }).catch((e) => { this.messageException(e); });
      },

      // 问题类型选择
      selectTaskType() {
        this.pageIndex = 1;
        this.page.current = 1;
        this.loadTaskList();
      },
      cleanSelectTaskType() {
        this.pageIndex = 0;
        this.selectTaskTypeValue = '';
        this.page.current = 1;
        this.loadTaskList();
      }
    },
    created() {
      this.pageIndex = 0;
      this.loadTaskList();
      this.loadSelectTaskTypeList();
    }
  }
</script>

<style lang="less" scoped>
  .will-task {
  }

  .select-task-type {
    margin-top: 10px;
    margin-left: 1%;
    margin-bottom: 10px;
    color: red;
  }
</style>
